import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:palmf_hid/common/values/values.dart';

import 'my_input_widget.dart';
import 'sync_data_select_logic.dart';

class SyncDataSelectPage extends StatefulWidget {
  const SyncDataSelectPage({Key? key}) : super(key: key);

  @override
  State<SyncDataSelectPage> createState() => _SyncDataSelectPageState();
}

class _SyncDataSelectPageState extends State<SyncDataSelectPage> {
  final logic = Get.put(SyncDataSelectLogic());
  final state = Get.find<SyncDataSelectLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
          height: 0.3.sh,
          width: 0.3.sw,
          decoration: const BoxDecoration(
            color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10))
          ),
          child: Column(
            children: [
              const Row(
                children: [
                  Spacer(),
                  Text(
                    "请选择同步数据方式",
                    style: TextStyle(color: MyColor.c_99000000),
                  ),
                  Spacer(),
                  CloseButton(),
                ],
              ),
              const Divider(),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 30,
                    width: 0.2.sw,
                    child: _buildProject("请输入同步条数"),
                  )
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              InkWell(
                  child: Obx(
                () => Container(
                    padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                    decoration: const BoxDecoration(
                        color: Colors.orange,
                        borderRadius: BorderRadius.all(Radius.circular(5))),
                    child: Text("同步最近${state.syncNum}条数据",
                        style: const TextStyle(
                            color: MyColor.c_FFFFFFFF, fontSize: 12)))),
                onTap: () {
                   if(state.syncNum=="0"){
                     MyToastUtil.show("同步数量必须大于0");
                     return;
                   }
                   int num=int.parse(state.syncNum);
                   if(num>1000){
                     MyToastUtil.show("同步数量小于1000");
                     return;
                   }
                  Get.back(result:num );
                },
              ),
              const Divider(),
              InkWell(
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                  decoration: const BoxDecoration(
                      color: MyColor.account_color,
                      borderRadius: BorderRadius.all(Radius.circular(5))),
                  child: const Text("同步全部数据",
                      style:
                          TextStyle(color: MyColor.c_FFFFFFFF, fontSize: 12)),
                ),
                onTap: () {
                  Get.back(result: -1);
                },
              ),
            ],
          )),
    );
  }

  ///
  _buildProject(String str) {
    return inputContainer(
      hintText: str,
      keyboardType: TextInputType.number,
      maxLength: 4,
      maxLines: 1,
      inputFormatters: [
        FilteringTextInputFormatter.allow(RegExp(r'[0-9]')) //设置只允许输入数字
      ],
      controller: logic.textEditingController,
      focusNode: logic.searchFocusNode,
      onChange: (str) {
        if(str==""){
          str="0";
        }
        state.syncNum = str;
        MyLogUtil().i(str);
      },
    );
  }

  @override
  void dispose() {
    Get.delete<SyncDataSelectLogic>();
    super.dispose();
  }
}
